package factory;

import java.text.NumberFormat;

/**
 * @author Steve
 * OrderObject class
 */
public class OrderObject {
	private static final String LINE_SIZE = "                                          ";
	private final String name;
	private final double price;
	private final boolean display;
	private OrderObject[] nextObject;

	/**
	 * @param name name of order item
	 * @param price price value
	 * @param display shows to display in print or not
	 */
	public OrderObject(String name, double price, boolean display){
		this(name, price, display, (OrderObject[])null); 
	}

	/**
	 * @param name name of order item
	 * @param price price value
	 * @param display shows to display in print or not
	 * @param oo orderObject
	 */
	public OrderObject(String name, double price, boolean display, OrderObject oo){
		this(name, price, display, (oo==null?null:new OrderObject[] { oo })); 
	}

	/**
	 * @param name name of order item
	 * @param price price value
	 * @param display shows to display in print or not
	 * @param oo orderObject array
	 */
	public OrderObject(String name, double price, boolean display, OrderObject[] oo){
		this.name = name;
		this.price = price;
		this.display = display;
		nextObject = (oo==null?new OrderObject[0]:oo);
	}

	/**
	 * @return name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return price
	 */
	public double getPrice() {
		return price;
	}

	/**
	 * @return next nextObject
	 */
	public OrderObject[] getNextObject() {
		return nextObject;
	}

	/** 
	 * @return order in a receipt fashion.  In tiers
	 */
	@Override
	public String toString(){
		return recursivePrint(""); 
	}

	/**
	 * Values are treated as a currency, values of 0.00 are not printed.  
	 * Uses spacing to line everything up in a tiered fashion.
	 * @param prefix before string
	 * @return string of one line of the order - name, value
	 */
	private String recursivePrint(String prefix){
		StringBuffer sb = new StringBuffer();

		//to display or not display item title
		if(display){
			String pre = prefix + name;
			String post = (price>0?formatPrice(price):"");
			String middle = LINE_SIZE.substring(pre.length() + post.length());

			sb.append(pre).append(middle).append(post).append("\n");

			prefix += "  ";
		}

		for(OrderObject oo : nextObject)
			sb.append(oo.recursivePrint(prefix));

		return sb.toString();
	}

	/**
	 * @return single string of entire order
	 */
	public String toLine(){
		return recursiveLine(","); 
	}

	/**
	 * Values are treated as a double, values of 0.00 are not printed.  
	 * @param prefix before string
	 * @return string of one line of the order - name, value
	 */
	private String recursiveLine(String prefix){
		StringBuffer sb = new StringBuffer();

		// adds an extra line when key titles are found
		if(name.equalsIgnoreCase("Drink"))
			sb.append("\n");
		else if(name.equalsIgnoreCase("Hot Food"))
			sb.append("\n");
		else if(name.equalsIgnoreCase("Bakery"))
			sb.append("\n");
		else if(name.equalsIgnoreCase("Ice Cream"))
			sb.append("\n");
		else if(name.equalsIgnoreCase("Sundae"))
			sb.append("\n");

		//to display or not display item title
		if(display){
			String pre = name + prefix;

			// don't show values equal to or less then zero
			String post = (price>0?price + ",":"");

			// don't show values less then zero
//			String post = (price>=0?price + ",":"");

			sb.append(pre).append(post);
		}

		for(OrderObject oo : nextObject)
			sb.append(oo.recursiveLine(prefix));

		return sb.toString();
	}

	/**
	 * @param p double price value 
	 * @return currency format double
	 */
	private static String formatPrice(double p){
		return NumberFormat.getCurrencyInstance().format(p);
	}
}
